import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class OpenCvTest2 {
    public static void main(String[] args) {
        //加载配置
        URL url = ClassLoader.getSystemResource("lib/opencv_java455.dll");
        System.load(url.getPath());
        //读入图片
        Mat imread = Imgcodecs.imread("E:\\openCv\\asd.jpg");
        Mat src = Imgcodecs.imread("E:\\openCv\\asd.jpg");

        //灰度化
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
        //横向
        Mat gray_x = new Mat();
        Imgproc.Sobel(src, gray_x, -1, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_x.jpg", gray_x);
        //竖向
        Mat gray_y = new Mat();
        Imgproc.Sobel(src, gray_y, -1, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_y.jpg", gray_y);
        //横竖向图像融合
        Mat gray_xy = new Mat();
        Core.subtract(gray_x, gray_y, gray_xy);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_xy.jpg", gray_xy);
        Mat handleSrc = Imgcodecs.imread("G:\\opencvPhoto\\result\\gray_xy.jpg");

//handle
        Core.convertScaleAbs(gray_xy, gray_xy);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\handle.jpg", gray_xy);
        Mat target = new Mat();

        // 保存二值化后图片

        Imgproc.adaptiveThreshold(gray_xy, target, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 909, -150);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\rzh.jpg", target);

        //定义矩阵
        Mat juzheng = new Mat();
        Mat shuchu = new Mat();
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 7));
        Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));

        Imgproc.morphologyEx(target, juzheng, Imgproc.MORPH_CLOSE, kernel);
        Imgproc.morphologyEx(juzheng, shuchu, Imgproc.MORPH_OPEN, kernel1);
        Imgproc.morphologyEx(shuchu, shuchu, Imgproc.MORPH_OPEN, kernel1);
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\juzheng.jpg", shuchu);

        List<MatOfPoint> contours = new ArrayList<>();
        Mat hierarchy = new Mat();
        double maxVal = 0;
        int maxValIdx = 0;
        Imgproc.findContours(shuchu, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
            double contourArea = Imgproc.contourArea(contours.get(contourIdx));
            if (maxVal < contourArea) {
                maxVal = contourArea;
                maxValIdx = contourIdx;

            }
        }
        MatOfPoint box = contours.get(maxValIdx);
        MatOfPoint2f box2 = new MatOfPoint2f(box.toArray());
        Imgproc.minAreaRect(box2);
        ArrayList<MatOfPoint> contour = new ArrayList<>();
        contour.add(box);
        Imgproc.drawContours(imread , contour, -1, new Scalar(255, 0, 0));
        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\quanjuzheng.jpg",imread );



    }
}

//灰度化
//        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
//        //横向
//        Mat gray_x = new Mat();
//        Imgproc.Sobel(src, gray_x, -1, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
//        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_x.jpg", gray_x);
//        //竖向
//        Mat gray_y = new Mat();
//        Imgproc.Sobel(src, gray_y, -1, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
//
//        //横竖向图像融合
//        Mat gray_xy = new Mat();
//        Core.subtract(gray_x, gray_y,  gray_xy);
//        Mat dst = new Mat();
//        Imgproc.blur(src, dst, new Size(3, 3), new Point(1, 1), Core.BORDER_DEFAULT);
//        Mat dst = new Mat();
//        //定义新矩阵
//        Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 909, -150);
//        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 7));
//        Mat kernel1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));
//
//        Imgproc.morphologyEx(mat, dst, Imgproc.MORPH_CLOSE, kernel,, iterations=1);
//        Imgproc.morphologyEx(mat, dilate, Imgproc.MORPH_OPEN, kernel2,  iterations=2);
//
//        List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
//        Mat hierarchy = new Mat();
//        Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
//
////        接着需要对contourArea(contours）排序：
//
//        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
//        {
//            double contourArea = Imgproc.contourArea(contours.get(contourIdx));
//            if (maxVal < contourArea)
//            {
//                maxVal = contourArea;
//                maxValIdx = contourIdx;
//            }
//        }
//
//        rRect = Imgproc.minAreaRect(mop2f);
//
//        Imgcodecs.imwrite("G:\\opencvPhoto\\result\\gray_xy.jpg", gray_xy);






